<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        #b1{
            background-color: yellow;
            width:150px;
            height:150px;

        }
    </style>
</head>
<body>
<script type="text/javascript">
    function test(obj){
        alert(obj.value);
    }
    function test1(){
        var obj = document.createElement("input");
        obj.type="button";
        obj.value="b2";
        //以下两句都可以
        obj.onclick=function(){test(this)};
        //obj.setAttribute("onclick",function(){test(this)});
        var b1 = document.getElementById("b1");
        document.body.insertBefore(obj, b1);
    }
</script>
<input type="button" id="b1" value="b1" onclick="test1()"/>
</body>
</body>
</html>